{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import smtplib\n",
    "from email.mime.multipart import MIMEMultipart\n",
    "from email import encoders\n",
    "from email.header import Header\n",
    "from email.mime.text import MIMEText\n",
    "from email.utils import parseaddr, formataddr\n",
    "from email.mime.application import MIMEApplication\n",
    "\n",
    "#发件人邮箱\n",
    "asender=\"zhangjunhongdata@163.com\"\n",
    "#收件人邮箱\n",
    "areceiver=\"zhangjunhong@163.com\"\n",
    "\n",
    "#抄送人邮箱\n",
    "acc = 'zhangjunhong@qq.com'\n",
    "\n",
    "#邮件主题\n",
    "asubject = '这是一份测试邮件'\n",
    "\n",
    "#发件人地址\n",
    "from_addr = \"zhangjunhongdata@163.com\"\n",
    "\n",
    "#邮箱密码（授权码）\n",
    "password=\"123data\"\n",
    "\n",
    "#邮件设置\n",
    "msg = MIMEMultipart()\n",
    "msg['Subject'] = asubject\n",
    "msg['to'] = areceiver\n",
    "msg['Cc'] = acc\n",
    "msg['from'] = \"张俊红\"\n",
    "\n",
    "#邮件正文\n",
    "body = \"你好，这是一份测试邮件\"\n",
    "\n",
    "#添加邮件正文:\n",
    "msg.attach(MIMEText(body, 'plain', 'utf-8'))\n",
    "\n",
    "#添加附件\n",
    "#注意这里的文件路径是斜杠\n",
    "xlsxpart = MIMEApplication(open('C:/Users/zhangjunhong/Desktop/这是附件.xlsx','rb').read())\n",
    "xlsxpart.add_header('Content-Disposition', 'attachment', filename='这是附件.xlsx')\n",
    "msg.attach(xlsxpart)\n",
    "\n",
    "#设置邮箱服务器地址以及端口\n",
    "smtp_server =\"smtp.163.com\"\n",
    "server = smtplib.SMTP(smtp_server, 25)\n",
    "server.set_debuglevel(1)\n",
    "\n",
    "#登录邮箱\n",
    "server.login(from_addr, password)\n",
    "\n",
    "#发送邮件\n",
    "server.sendmail(from_addr, areceiver.split(',')+acc.split(','),msg.as_string())\n",
    "\n",
    "#断开服务器连接\n",
    "server.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import smtplib\n",
    "from email.mime.multipart import MIMEMultipart\n",
    "from email.mime.text import MIMEText\n",
    "from email.mime.image import MIMEImage\n",
    "from email.mime.application import MIMEApplication\n",
    "\n",
    "host = \"smtp.163.com\"\n",
    "port = 25\n",
    "username = \"zhangjunhong1227@163.com\"\n",
    "password = \"123zjh\"\n",
    "\n",
    "smtp = smtplib.SMTP() #声明一个链接对象\n",
    "smtp.connect(host, port) # 与服务器进行连接\n",
    "smtp.set_debuglevel(1) #显示出交互信息\n",
    "smtp.login(username, password) # 登陆邮箱\n",
    "\n",
    "sender = username\n",
    "\n",
    "for i in zip(df[\"姓名\"],df[\"收件人\"],df[\"抄送人\"]):\n",
    "    receiver = i[1] #收件人\n",
    "    acc = i[2] #抄送人\n",
    "    \n",
    "    msg = MIMEMultipart() #声明一个邮件对象\n",
    "    msg['from'] = username #发件人\n",
    "    msg['to'] = receiver#收件人\n",
    "    msg['Cc'] = acc #抄送人\n",
    "    msg['Subject'] = i[0] + \"任务明细\" #主题\n",
    "    \n",
    "    # 编写正文\n",
    "    text = MIMEText(i[0]+\"您好，这是您这个月的任务明细\",'plain', 'utf-8')\n",
    "    msg.attach(text)\n",
    "    \n",
    "    # 添加表格附件\n",
    "    f = open('C:/Users/zhangjunhong/Desktop/ 任 务 明 细 /'+ i[0] + '.xlsx','rb').read()\n",
    "    filepart = MIMEApplication(f)\n",
    "    filepart.add_header('Content-Disposition','attachment',filename=i[0] + '任务明细.xlsx') #为附件添加一个标题\n",
    "    msg.attach(filepart)\n",
    "    \n",
    "    smtp.sendmail(sender, receiver.split(',') + acc.split(','), msg.as_string())# 发送邮件\n",
    "smtp.quit() # 断开服务器连接"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
